package com.leanyuan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.domain.TParkingspace;
import com.leanyuan.publi.domain.TParkinglot;
import com.leanyuan.publi.service.TParkinglotService;
import com.leanyuan.service.TParkingspaceService_V;
import com.leanyuan.vo.ParkingspaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/t-parkingspace")
public class TParkingspaceController {
    @Autowired
    TParkingspaceService_V tParkingspaceServiceV;

    @Autowired
    TParkinglotService tParkinglotService;

    //查询所有停车位信息并分页
    @RequestMapping("selectPage")
    public IPage<ParkingspaceVo> selectPage(Integer page, Integer pageSize, Model model){
        Page<ParkingspaceVo> iPage=new Page<ParkingspaceVo>(page,pageSize);
        IPage<ParkingspaceVo> tParkingspacePage= this.tParkingspaceServiceV.getAll1(iPage);
        model.addAttribute("tParkingspacePage",tParkingspacePage);
        return tParkingspacePage;
    }

    //查询所有停车场信息
    @RequestMapping("selectParkinglot")
    public List<TParkinglot> selectParkinglot(Model model){
        List<TParkinglot> tParkinglot=this.tParkinglotService.list(null);
        model.addAttribute("tParkinglot",tParkinglot);
        return tParkinglot;
    }

    //添加停车位
    @RequestMapping( "addParkingspace")
    public boolean addParkingspace(@RequestBody TParkingspace tParkingspace){
        QueryWrapper<TParkingspace> queryWrapper =new QueryWrapper<TParkingspace>();
        tParkingspace.setParkingspaceState("空闲");
        boolean bole = this.tParkingspaceServiceV.save(tParkingspace);
        queryWrapper.eq("parkingspace_number",tParkingspace.getParkingspaceNumber());

        List<TParkingspace> tParkingspace1 =this.tParkingspaceServiceV.list(queryWrapper);
        if(tParkingspace1.size()>=2){
            this.tParkingspaceServiceV.removeById(tParkingspace.getParkingspaceId());
            return  false;
        }
        return bole;
    }

    //根据ID删除停车场
    @RequestMapping( "deleteParkingspaceById")
    public boolean deleteParkingspaceById(Integer id){
        return this.tParkingspaceServiceV.removeById(id);
    }

    //根据id修改信息
    @RequestMapping("updateParkingSpaceById")
    public boolean updateParkingSpaceById(@RequestBody TParkingspace tParkingspace){
        QueryWrapper<TParkingspace> queryWrapper =new QueryWrapper<TParkingspace>();
        queryWrapper.eq("parkingspace_number",tParkingspace.getParkingspaceNumber());

        QueryWrapper<TParkingspace> queryWrapper1 =new QueryWrapper<TParkingspace>();
        queryWrapper1.eq("parkingspace_id",tParkingspace.getParkingspaceId());

        TParkingspace tParkingspace2=this.tParkingspaceServiceV.getOne(queryWrapper1);

        boolean bole = this.tParkingspaceServiceV.updateById(tParkingspace);

        List<TParkingspace> tParkingspace1 =this.tParkingspaceServiceV.list(queryWrapper);
        if(tParkingspace1.size()>=2){
            this.tParkingspaceServiceV.updateById(tParkingspace2);
            return  false;
        }
        return bole;
    }

    //带条件的查询分页
    @RequestMapping("selectPageParkingspaceByParam")
    public IPage<ParkingspaceVo> selectPageParkingspaceByParam(String parkingspaceNumber, String parkingSpaceStatc, Integer parkinglotid, Integer page, Integer pageSize, Model model){
        //分页
        Page<ParkingspaceVo> iPage=new Page<ParkingspaceVo>(page,pageSize);

        if(parkingspaceNumber == ""){parkingspaceNumber=null;}
        if(parkingSpaceStatc == ""){parkingSpaceStatc=null;}
        if(parkinglotid == -1){parkinglotid=null;}


        IPage<ParkingspaceVo> tParkingspaceIPage= this.tParkingspaceServiceV.getAll2(iPage,parkingspaceNumber,parkingSpaceStatc,parkinglotid);
        model.addAttribute("tParkingspaceIPageParam",tParkingspaceIPage);
        return tParkingspaceIPage;
    }
}

